/**
 * 1.所有的操作在这里
 * 已分诊和未分诊
 * **/
/**
 * 重置按钮
 * */
function resetBtn() {
    $('.btn-tools button').addClass('layui-btn-disabled').prop('disabled',true);
}

function autoXcbd() {
    var t = setTimeout(function () {
        xcbdAct();
        clearTimeout(t);
        t = null;
    },3000);
}

/**
 *  报到操作
 * */
function xcbdAct(patientKey,from) {
    patientKey = patientKey ||'';
    from = from || '';
    // 优化，如果弹窗打开过，就不重复打开了
    if (parent && !parent.layer.getFrameIndex('xcbdModel') ) {
        parent.layer.open({
            id: 'xcbdModel',
            type: 2,
            title: "现场报到",
            shadeClose: true,
            closeBtn: 1,
            area: ['480px', '395px'],
            content: './window/livecoverage_win.html?patientKey=' + patientKey + '&from=' + from
        });
    }
}
/**
 *  录入患者
 * **/
function addPatinet() {
    var winId = "enterWin_new";
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: '录入患者信息',
        shadeClose: false,
        closeBtn: 1,
        area: ['480px', '720px'],
        content: './window/enter_win.html?paientId=',
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.enterOrUpdatePatient(0,function (res) {
                if(res.status==0){
                    layer.msg("录入成功", { icon: 1 });
                    parent&&parent.layer.close(index);
                }else {
                    layer.msg("录入失败", { icon: 2 });
                }
                resetBtn();
            });
        },
        end: function () {
            refreshTab();
        }
    });
}
/**
 *  编辑患者
 * **/
function modPatinet() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var winId = "enterWin_edit";
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: '录入患者信息',
        shadeClose: false,
        closeBtn: 1,
        area: ['480px', '720px'],
        content: './window/enter_win.html?paientId=' + data.id + "&queueId=" + getLocalQueueId() + "&queueKey=" + localStorage["queueKey"] + "&systemId=" + localStorage["systemId"],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.enterOrUpdatePatient(1,function (res) {
                if(res.status==0){
                    layer.msg("录入成功", { icon: 1 });
                    parent&&parent.layer.close(index);
                }else {
                    layer.msg("录入失败", { icon: 2 });
                }
            });
        },
        end: function () {
            refreshTab();
        }
    });
}
/***
 *  找人
 * */
function findOne() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var _gbmsg = "请[n2]" + data.registerid + "[n0]号" +  data.patientname + "[p200]到分诊台";
    $.ajax({
        url: _broadcastUrl,
        type: 'get',
        data: {
            queueId: getLocalQueueId(),
            hospitalId: getHospitalId(),
            message: _gbmsg,
        },
        success: function (d) {
            var _status = JSON.parse(d).status;
            var _desc = JSON.parse(d).desc;
            if (_status == 0) {
                parent&&parent.layer.alert("广播成功", { icon: 1 });
            } else {
                parent&&parent.layer.alert(_desc, { icon: 2 });
            }
        }
    });
}
/**
 * 语音播放  广播
 * **/
function voiceBroadcast() {
    parent&&parent.layer.open({
        type: 2,
        title: '语音播报',
        shadeClose: true,
        area: ['80%', '60%'],
        content: './window/voice.html'
    });
}
/***
 *  语音对讲
 * */
function openCalling() {
    parent.layer.open({
        type: 2,
        title: "对讲",
        btnAlign:"c",
        area: ['650px','100%'],
        offset: "r",
        move: false,
        content: './window/calling.html',
        end:function () {
            //关闭弹框更新是否勿扰模式
            isSwitchCalling();
        }
    });
}

/**
 * 移动患者
 * **/
function movePatient(type) {
    var winId = "moveUp_Win";
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var hospitalId = getHospitalId();
    var queueId = getLocalQueueId();
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: '移动患者',
        shadeClose: false,
        closeBtn: 1,
        area: ['350px', '185px'],
        content: './window/move_patient_win.html?movemodel='+type+'&id=' + data.id + '&queueId=' + queueId + '&hospitalId=' + hospitalId,
        btn: ['确定', '取消'],
        yes: function (index) {
            parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.save(function (res) {
                if(res.status==0){
                    parent&&parent.layer.close(index);
                    parent&&parent.layer.msg("操作成功", { icon: 1 });
                    refreshTab();
                }else {
                    parent&&parent.layer.msg("无法移动", { icon: 2 });
                }
                resetBtn();
            });
        },
        end: function () {
            refreshTab();
        }
    });
}

/***
 *   优先
 * */
function yxcz() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var buttonClicked = false;
    // 是否开启优先类别 选择，0：关闭，1：开启（2.10新增）  在子系统中配置
    var firstCategory = localStorage.getItem('firstCategory');
    if(firstCategory==0){
        parent&&parent.layer.confirm('确定对当前患者做优先操作吗',{
            btn: ['确定', '取消'],
            icon: 7,
        },function (index) {
            firstPatient(data.id,0,index);
        },function (index) {
            parent&&parent.layer.close(index);
        });
    }else {
        var dic = localStorage.getItem('ym_dic')||'[]';// 获取优先字典
        dic = JSON.parse(dic);
        if(dic.length>1){
            var frag = '<form class="layui-form" action=""><div class="layui-form-item">';
            frag +='<div class="layui-row" style="padding: 10px 15px">';
            for (var i =0;i<dic.length;i++){
                if(!dic[i]||dic[i]=='') continue;
                frag +='<input type="radio"  name="dics" value="'+dic[i]['dicKey']+'" title="'+dic[i]['dicName']+'" />';
            }
            frag +='</div>';
            frag +='</div></form>';
            layui.use('form', function(){
                parent&&parent.layer.open({
                    type: 1,
                    title: '请选择患者优先类别',
                    shadeClose: true,
                    area: ['360px', '240px'],
                    btn: ['确定', '取消'],
                    content: frag,
                    yes: function (index) {
                        if (!buttonClicked) {
                            buttonClicked = true;
                            var checked = parent&&parent.getDicCheckVal();
                            firstPatient(data.id,checked,index);
                        }
                    }
                });
                parent.initLayui();
            });
        }else if(dic.length==1){
            parent&&parent.layer.confirm('确定对当前患者做优先操作吗',{
                closeBtn: 0,
                btn: ['确定', '取消'],
                icon: 7,
                cancel: function () {
                },
                yes: function (index) {
                    firstPatient(data.id,dic[0]['dicKey'],index);
                }
            });
        }
    }
}

/***
 *  优先患者
 *  @param ids 队列排队id
 *  @param dicKey 优先的类型，关闭时传递 0
 *  @param index
 *
 * */
function firstPatient(ids,dicKey,index) {
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    $.ajax({
        url: _priorityPatients,
        type: "post",
        data: {
            id: ids,
            loginId: loginId,
            hospitalId: hospitalId,
            dicKey:dicKey
        },
        success: function (data) {
            var res = JSON.parse(data);
            if(res.status==0){
                parent&&parent.layer.msg(res.desc, { icon: 1 });
                parent&&parent.layer.close(index);
            }else {
               parent&&parent.layer.msg(res.desc, { icon: 2 });
            }
        },
        error: function () {
            layer.alert("操作失败!", { icon: 2 });
        },
        complete: function () {
            resetBtn()
        }
    });
}

function qxyx() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    var buttonClicked = false;
    parent&&parent.layer.confirm('确定对当前患者做取消优先操作吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                $.ajax({
                    url: _cnacelPriorityPatients,
                    type: "post",
                    data: {
                        id: data.id,
                        loginId: loginId,
                        hospitalId: hospitalId
                    },
                    success: function (data) {
                        var res = JSON.parse(data);
                        if(res.status==0){
                            parent&&parent.layer.msg(res.desc, { icon: 1 });
                            parent&&parent.layer.close(index);
                        }else {
                            parent&&parent.layer.msg(res.desc, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败!", { icon: 2 });
                    },
                    complete: function () {
                        resetBtn()
                    }
                });
            }
        }
    });
}
/***
 *  转诊操作
 * **/
function zzcz() {
    var data = getCheckedPatients();
    var queueId = getLocalQueueId();
    var queueKey = getLocalQueueKey();
    var systemId = allOrSingleSystemId();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var winId = "turnWin";
    var buttonClicked = false;
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: false,
        shadeClose: false,
        closeBtn: 0,
        area: ['360px', '460px'],
        content: './window/turndiagnosis_win.html?ids=' + data.id + "&queueId=" + queueId + "&systemId=" + systemId + "&queueKey=" + queueKey,
        btn: ['确定', '取消'],
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.setTransferQueue(function (res) {
                    if (res.status==0) {
                        layer.msg(res.desc,{icon:1});
                        parent&&parent.layer.close(index);
                        refreshTab();
                    }else {
                        buttonClicked = false;
                        layer.msg(res.desc);
                    }
                    resetBtn();
                });

            }
        },
        cancel: function () {

        }
    });
}

/**
 *  挂起操作
 * **/
function gqcz() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var buttonClicked = false;
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    parent&&parent.layer.confirm('确定对当前患者做挂起操作吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                $.ajax({
                    url: _hangupPatients,
                    type: "post",
                    data: {
                        id: data.id,
                        loginId: loginId,
                        hospitalId: hospitalId
                    },
                    success: function (data) {
                        var res = JSON.parse(data);
                        if(res.status==0){
                            parent&&parent.layer.msg(res.desc, { icon: 1 });
                            parent&&parent.layer.close(index);
                        }else {
                            parent&&parent.layer.msg(res.desc, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败!", { icon: 2 });
                    },
                    complete: function () {
                        resetBtn()
                    }
                });
            }
        }
    });
}
function qxgq() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    var buttonClicked = false;
    parent&&parent.layer.confirm('确定对当前患者做取消挂起操作吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                $.ajax({
                    url: _cancelHangupPatients,
                    type: "post",
                    data: {
                        id: data.id,
                        loginId: loginId,
                        hospitalId: hospitalId
                    },
                    success: function (data) {
                        var res = JSON.parse(data);
                        if(res.status==0){
                            parent&&parent.layer.msg(res.desc, { icon: 1 });
                            parent&&parent.layer.close(index);
                        }else {
                            parent&&parent.layer.msg(res.desc, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败!", { icon: 2 });
                    },
                    complete: function () {
                        resetBtn()
                    }
                });
            }
        }
    });
}


function yscz() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var winId = "delayWin";
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    var queueKey = getQueueKey();
    var buttonClicked = false;
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: '延时',
        shadeClose: false,
        area: ['400px', '220px'],
        content: './window/delay_win.html?id=' + data.id + "&hospitalId=" + hospitalId + "&loginId=" + loginId + "&queueKey=" + queueKey,
        btn: ['确定'],
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.confirmDelay(function (res) {
                    if (res.status==0) {
                        parent&&parent.layer.close(index);
                        layer.msg(res.desc,{icon:1});
                    }else {
                        buttonClicked = false;
                        parent&&parent.layer.msg(res.desc,{icon:0});
                    }
                    resetBtn();
                });

            }
        }
    });
}
function qxys() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    var queueKey = getLocalQueueKey();
    var buttonClicked = false;
    parent&&parent.layer.confirm('确定对当前患者做取消延时操作吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                $.ajax({
                    url: _cancelDelayPatients,
                    type: "post",
                    data: {
                        id: data.id,
                        loginId: loginId,
                        hospitalId: hospitalId,
                        queueKey: queueKey
                    },
                    success: function (data) {
                        var res = JSON.parse(data);
                        if(res.status==0){
                            parent&&parent.layer.msg(res.desc, { icon: 1 });
                            parent&&parent.layer.close(index);
                        }else {
                            parent&&parent.layer.msg(res.desc, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败!", { icon: 2 });
                    },
                    complete: function () {
                        resetBtn()
                    }
                });
            }
        }
    });
}

function fzhz() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    var queueKey = getLocalQueueKey();
    var triageId = getDeviceId();
    var systemId = allOrSingleSystemId();
    var deptId = parent&&parent.allOrSingleDeptId();
    var buttonClicked = false;
    var fenzFzType = "fz";
    var fenzFzSignId = "fzY";
    var pretestStage =  localStorage["colorStatus"];
    if(pretestStage==1){
        pretestStage = data.preteststage;
    }else {
        pretestStage = 3;
    }
    parent&&parent.layer.confirm('确定对当前患者做【复诊】操作吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                var winId = "triageWin";
                if (!checkQueueIsOffline(localStorage["queueId"])) {
                    // 如果当前队列非离线，判断复诊时初诊指定医生是否在线
                    var doctorInfo = doctorNameInfo(data);
                    var doctorId = doctorInfo.id;//初诊时的医生
                    var doctName = doctorInfo.name;//初诊时的医生
                    var doctStatus = doctorInfo.status;//初诊医生是否在线 2离线 0 在线 1 暂停
                    if (doctStatus == 2) {
                        parent&&parent.layer.open({
                            id: winId,
                            type: 2,
                            title: false,
                            shadeClose: false,
                            closeBtn: 0,
                            area: ['400px', '280px'],
                            content: './window/triage_win.html?queueKey=' + queueKey + "&patientKey=" + data.patientkey + "&systemId=" + systemId + "&deptId=" + deptId + "&pretestStage=" + pretestStage + "&ids=" + data.id + "&fenzFzType=" + fenzFzType + "&fenzFzSignId=" + fenzFzSignId,
                            btn: ['确定', '取消'],
                            yes: function (index) {
                                parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.checkBack(function (res) {
                                    if (res.status==0) {
                                        parent&&parent.layer.close(index);
                                        parent&&parent.layer.msg(res.desc,{icon:1});
                                    }else {
                                        buttonClicked = false;
                                        parent&&parent.layer.msg(res.desc,{icon:0});
                                    }
                                    resetBtn();
                                });
                            }
                        });
                    } else {//医生在线
                        $.ajax({
                            url: _checkBack,
                            type: "post",
                            data: {
                                id: data.id,
                                loginId: loginId,
                                hospitalId: hospitalId,
                                triageId: triageId,
                                queueKey: queueKey,
                                queueId: data.queueid,
                                patientKey: data.patientkey
                            }, success: function (d) {
                                var jsonObj = JSON.parse(d);
                                if (jsonObj.status == 0) {
                                    var signId = jsonObj.data;
                                    triagePatients(signId, pretestStage, doctorId, doctName,index);
                                } else {
                                    //异常的提示信息
                                    parent&&parent.layer.alert(jsonObj.desc);
                                }
                            },
                            error: function () {
                                parent&&parent.layer.alert("操作失败!", { icon: 2 });
                            },
                            complete: function () {
                                resetBtn()
                            }
                        });
                    }
                }else {
                    // 如果当前队列离线，请用户选择分诊队列
                    parent&&parent.layer.open({
                        id: winId,
                        type: 2,
                        title: false,
                        shadeClose: false,
                        closeBtn: 0,
                        area: ['400px', '280px'],
                        content: './window/triage_win.html?queueKey=' + queueKey + "&patientKey=" + patientKey + "&systemId=" + systemId + "&deptId=" + deptId + "&pretestStage=" + pretestStage + "&ids=" + ids + "&fenzFzType=" + fenzFzType + "&fenzFzSignId=" + fenzFzSignId,
                        btn: ['确定', '取消'],
                        yes: function (index) {
                            parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.checkBack(function (res) {
                                if (res.status==0) {
                                    parent&&parent.layer.close(index);
                                    parent&&parent.layer.msg(res.desc,{icon:1});
                                }else {
                                    buttonClicked = false;
                                    parent&&parent.layer.msg(res.desc,{icon:0});
                                }
                                resetBtn();
                            });
                        }
                    });
                }
            }
        }
    });
}

// 对患者做分诊操作
// signId: 患者的报到id
function triagePatients(signId, pretestStage, doctorId, doctorName,index) {
    var queueKey = localStorage["queueKey"];
    var loginId = getLoginUserId();
    var hospitalId = getHospitalId();
    $.ajax({
        url: _triagePatients,
        type: "post",
        data: {
            hospitalId: hospitalId,
            signIds: signId,
            queueKey: queueKey,
            editor: loginId,
            pretestStage: pretestStage,
            doctorId: doctorId,
            doctorName: doctorName
        },
        success: function (data) {
            var result = JSON.parse(data);
            if (result.status != 0) {
                parent&&parent.layer.msg(result.desc, { icon: 1 });
            } else {
                parent&&parent.layer.close(index);
                parent&&parent.layer.alert("复诊-自动分诊成功", { icon: 1, zIndex: 198920004, time: 15000 });
            }
        },
        error: function () {
            layer.alert("对患者分诊操作失败!", { icon: 2, zIndex: 198920004 });
        },
        complete: function () {
            resetBtn()
        }
    });
}

function doctorNameInfo(data) {
    // var deptId = parent.$("#select_call_systemType option:selected").attr("data-deptId"); // 父页面的科室id
    var deptId = parent&&parent.allOrSingleDeptId();
    var hospitalId = getHospitalId();
    var queueId = data.queueid;
    var patientKey =  data.patientkey;
    var doctorInfo = {
        id:'',
        name:'',
        status:'',
    };

    $.ajax({
        url: getDoctorByQueueIdUrl,
        data: {
            deptId: deptId,
            hospitalId: hospitalId,
            queueId: queueId,
            patientKey: patientKey
        },
        type: 'get',
        async: false,
        success: function (d) {
            if (JSON.parse(d).status == '0') {
                var dataInfo = JSON.parse(d).data;
                var lengthList = dataInfo.length;
                for (var i = 0; i < lengthList; i++) {
                    var _doctorId = dataInfo[i].doctorId;
                    var _doctorName = dataInfo[i].doctorName;
                    var _loginStatus = dataInfo[i].loginStatus;//初诊医生展示字段  0：在线 1：暂停  2：离线
                    if (_loginStatus == 1 || _loginStatus == 0 || _loginStatus == 2) {
                        doctorInfo.id = _doctorId;
                        doctorInfo.name = _doctorName;
                        doctorInfo.status = _loginStatus;
                    }

                }
            }
        }
    });
    return doctorInfo;
}





function lstd() {
    var page = localStorage.getItem("_ISNOSPLIT_PAGE_");
    var data = getCheckedPatients();
    var params = {
        id: data.id,
        greenChannelSource: 1
    };
    if(page=='Y'){
        // 这里是未分诊
        if(!data||!data.signids||data.signids=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
        params = {
            id: data.signids,
            greenChannelSource: 0
        };
    }else {
        if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    }
    var buttonClicked = false;
    parent&&parent.layer.confirm('确定将当前患者设置为绿色通道吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                $.ajax({
                    url: _greenPipePatients,
                    type: "post",
                    data: params,
                    success: function (data) {
                        var res = JSON.parse(data);
                        if(res.status==0){
                            parent&&parent.layer.msg(res.desc, { icon: 1 });
                            parent&&parent.layer.close(index);
                        }else {
                            parent&&parent.layer.msg(res.desc, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败!", { icon: 2 });
                    },
                    complete: function () {
                        resetBtn()
                    }
                });
            }
        }
    });
}
function zhcz() {
    var data = getCheckedPatients();
    if(!data||!data.id||data.id=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var buttonClicked = false;
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    var systemId = allOrSingleSystemId();
    parent&&parent.layer.confirm('确定对当前患者做【过号召回】操作吗',{
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7,
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
                $.ajax({
                    url: _recallPatients,
                    type: "post",
                    data: {
                        id: data.id,
                        loginId: loginId,
                        hospitalId: hospitalId,
                        systemId: systemId
                    },
                    success: function (data) {
                        var res = JSON.parse(data);
                        if(res.status==0){
                            parent&&parent.layer.msg(res.desc, { icon: 1 });
                            parent&&parent.layer.close(index);
                        }else {
                            parent&&parent.layer.msg(res.desc, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败!", { icon: 2 });
                    },
                    complete: function () {
                        resetBtn()
                    }
                });
            }
        }
    });
}
function fzcz() {
    var data = getCheckedPatients();
    if(!data||!data.patientkey||data.patientkey=='') return  layer.msg("请选择一个患者", { icon: 7, time: 1000 });
    var winId = "triageWin";
    var buttonClicked = false;
    var fenzFzSignId = "fenzY";
    var fenzFzType = "fenz";
    if(data.signtype==2) fenzFzType = "fenz";
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: false,
        shadeClose: false,
        closeBtn: 0,
        area: ['400px', '300px'],
        content: './window/triage_win.html?systemId=' + getSystemId() + "&deptId=" + getDeptId()+ "&signId=" + data.signids+ "&queueKey=" + data.registerobjkey+ "&patientKey=" + data.patientkey+"&fenzFzType=" + fenzFzType+"&fenzFzSignId=" + fenzFzSignId,
        btn: ['确定', '取消'],
        yes: function (index) {
            if (!buttonClicked) {
                buttonClicked = true;
//                  var result = true;
                parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.checkBack(function (res) {
                    if (res.status==0) {
                        parent&&parent.layer.close(index);
                        parent&&parent.layer.msg(res.desc,{icon:1});
                        getWfzList();
                    }else {
                        buttonClicked = false;
                        parent&&parent.layer.msg(res.desc,{icon:0});
                    }
                    resetBtn();
                });
            }
        }
    });
}
function unbindDoc(self) {
    var data = $(self).parents('ul').data();
    var hospitalId = getHospitalId();
    var loginId = getLoginUserId();
    parent&&parent.layer.confirm("确认将为此患者取消接诊医生的绑定吗？", {
        closeBtn: 0,
        btn: ['确定', '取消'],
        icon: 7
    }, function (index) {
        $.ajax({
            url: _delDoctorUrl,
            type: "post",
            data: {
                hospitalId: hospitalId,
                id: data.id,
                loginId: loginId,
                queueId: data.queueid
            },
            success: function (data) {
                parent&&parent.layer.close(index);
                var res = JSON.parse(data);
                if (res.status == 0) {
                    layer.msg("操作成功!", { icon: 1, time: 2000 });
                    // $(_this).parent().html("");
                }else {
                    layer.msg(res.desc, { icon: 0, time: 2000 });
                }
            },
            error: function () {
                parent&&parent.layer.alert("操作失败", { icon: 2 });
            }
        });
    }, function (index) {
        layer.close(index);
    });
}

function batchChecks() {
    try{
        my_cefsharp.Set_ReadCard_Mode("1");
    }catch (e) {
    }
    var winId = "bulkPageTmt";
    parent&&parent.layer.open({
        id: winId,
        type: 2,
        title: "批量报到",
        shadeClose: false,
        closeBtn: 0,
        area: ['800px', '700px'],
        content: './window/batch_register.html'
    });
}
function coloseBatchChecks() {
    var winId = "bulkPageTmt";
    parent&&parent.document.getElementById(winId).getElementsByTagName('iframe')[0].contentWindow.patientCount(function (result) {
        var msg = "当前无患者报到，是否确认关闭？";
        if (result > 0) {
            msg = "尚有患者未报到，是否确认关闭？";
        }
        parent.layer.confirm(msg, {
            icon:7,
            btn: ['确定', '取消'] //按钮
        }, function () {
            try{
                my_cefsharp.Set_ReadCard_Mode("0");
            }catch (e) {

            }
            parent.layer.closeAll();
        });
    });
}












function getCheckedPatients() {
    var idx = $('#nurse_tabs li.active').index();
    var data = {};
    if(idx==0) {
        data = $('#waitList input[name="sel_radio"]:checked').parents('ul').data();
    } else if(idx==1){
        data = $('#fzList input[name="sel_radio"]:checked').parents('ul').data();
    }else if(idx==2){
        data = $('#yjzList input[name="sel_radio"]:checked').parents('ul').data();
    }else {
        data = $('#wfzList input[name="sel_radio"]:checked').parents('ul').data();
    }
    return data;
}
